package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.bean.User;
import edu.neusoft.yuejian_09.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends Activity {
    private Button btn_reg,btn_regreturn;
    private EditText username_reg,pass_reg,user_telephone,user_email,surepass_reg;
    private ImageView yuejianlogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_reg);
        //调用函数
        initViews();
        initEvents();
        initData();

    }
    public void initViews()//控件管理
    {
        //初始化控件
        btn_reg= (Button) findViewById(R.id.btn_reg);
        username_reg=(EditText)findViewById(R.id.username_reg);
        pass_reg=(EditText)findViewById(R.id.pass_reg);
        surepass_reg=(EditText)findViewById(R.id.surepass_reg);
        user_telephone=(EditText)findViewById(R.id.user_telephone);
        user_email=(EditText)findViewById(R.id.user_email);
        btn_regreturn=(Button) findViewById(R.id.btn_regreturn);

    }

    public void initEvents()//事件管理
    {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UserModel userModel=new UserModel();
                Call call=userModel.reg(
                        username_reg.getText().toString(),
                        pass_reg.getText().toString(),

                        user_telephone.getText().toString(),
                        user_email.getText().toString());



            Callback<User> callback = new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {

                    String success = response.body().getSuccess();

                    if ("0".equals(success)) {
                        Toast toast = Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT);
                        toast.show();
                    } else {
                        String str_user = username_reg.getText().toString();
                        String str_password = pass_reg.getText().toString();
                        String user_phone = user_telephone.getText().toString();
                        String user_intro = user_email.getText().toString();
                        Toast toast = Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT);
                        toast.show();
                        Intent intent = new Intent();
                        intent.putExtra("user_name", str_user);
                        intent.putExtra("user_password", str_password);
                        intent.putExtra("user_phone",user_phone);
                        intent.putExtra("user_intro",user_intro);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {

                }

            };

          call.enqueue(callback); }

        });




    }
    public void initData()//数据管理
    {

    }
}
